package com.atguigu.demo.controller;

import com.alibaba.fastjson.JSON;
import com.atguigu.demo.bean.Customer;
import com.atguigu.demo.service.CustomerService;
import com.atguigu.demo.service.impl.CustomerServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController  //@Controller 返回的是页面名称 ，但是一般已经不常用了
public class CustomerController {

    @Autowired
    CustomerService customerService;

    //http://localhost:8080/customer?name=zhang3&age=33
    @GetMapping("/customer")//只支持get请求  //@RequestMapping("/customer") 可以支持其他类型的请求比如post ..
    public List<Customer> getCustomerByName(@RequestParam("name")String name ,@RequestParam(value = "age" ,required = false ,defaultValue = "0") Integer age  ){

        List<Customer> customerList = customerService.list(new QueryWrapper<Customer>().eq("name", name).eq("age", age));

        return customerList;
    }

    //http://localhost:8080/customer/101.html
    @GetMapping("/customer/{id}.html")//只支持get请求  //@RequestMapping("/customer") 可以支持其他类型的请求比如post ..
    public Customer getCustomerByName(@PathVariable("id")String id    ){

        Customer customer = customerService.getById(id);
        return customer ;
    }

    //http://localhost:8080/customer 接收请求体（payload)的参数
    @PostMapping("/customer")
    public String saveCustomer(@RequestBody Customer customer ){
        System.out.println(JSON.toJSONString(customer)  );
        customer.setId("1018_4");
        customerService.saveCustomer(customer);

        customer.setId("1118_4");
        customerService.saveOrUpdate(customer);


        return  "success";
    }


}
